//
//  SelectLevelLayer.m
//  MinusTheBox
//
//  Created by Chi Ho Chiu on 30/4/12.
//  Copyright 2012 Individual. All rights reserved.
//

#import "SelectLevelLayer.h"
#import "GameManager.h"

@implementation SelectLevelLayer
@synthesize delegate;
@synthesize activeSubLayer;
@synthesize subLayer1;
@synthesize subLayer2;

-(void) dealloc
{
	[super dealloc];
}

#pragma mark - init
-(id) init
{
	if( self = [super init] )
	{
		self.isTouchEnabled = YES;
		
		CGSize winSize = [[CCDirector sharedDirector] winSize];
		
		//Background Image
		CCSprite *bg = [CCSprite spriteWithFile:@"mainmenu_bg.png"];
		bg.position = ccp(winSize.width/2, winSize.height/2);
		[self addChild:bg];
		
		//Title
		CCLabelBMFont *titleLabel = [CCLabelBMFont labelWithString:@"Level" fntFile:@"Dakota_white_24.fnt"];
		titleLabel.position = ccp(winSize.width/2, winSize.height - 20 );
		
		//Back Button
        CCLabelBMFont *backFont = [CCLabelBMFont labelWithString:@"Back" fntFile:@"Dakota_white_16.fnt"];
        CCMenuItem *backItem = [CCMenuItemLabel itemWithLabel:backFont target:self selector:@selector(onItemSelected:)];
        backItem.tag = kTAG_MENU_ITEM_BACK;
				
        CCMenu *backMenu = [CCMenu menuWithItems:backItem,  nil];
        [backMenu alignItemsVertically];
        [backMenu setPosition: ccp(20, winSize.height - 20 )];
		
		//Init SubLayer1, and lazy init SubLayer2
		[self initSubLayer1];
		
		//Assign active layer
		self.activeSubLayer = subLayer1;
		
		//Add Child
		[self addChild:subLayer1];
		[self addChild:titleLabel];
		[self addChild:backMenu];
	}
	
	return self;
}

-(void) onItemSelected:(id)sender
{
	switch ([sender tag])
	{
		case 1001:
			if(activeSubLayer == subLayer2)
			{
				[self backToSubLayer1];
			}
			else if(activeSubLayer == subLayer1)
			{
				//TODO: Back to Main Menu
				[self backToMainMenu];
			}
			break;
		case 1002:	//Level A
			[self switchToSubLayer2WithLevel:1];
			break;
		case 1003:	//Level B
			[self switchToSubLayer2WithLevel:21];
			break;
		case 1004:	//Level C
			[self switchToSubLayer2WithLevel:41];
			break;
		case 1005:	//Level D
			[self switchToSubLayer2WithLevel:61];
			break;
		case 1006:	//Level E
			[self switchToSubLayer2WithLevel:81];
			break;
		default:
			[[GameManager sharedGameManager] playLevel:[sender tag]];
			NSLog(@"Start Level: %d", [sender tag]);
			break;
	}
}

-(void) initSubLayer1
{
	CGSize winSize = [[CCDirector sharedDirector] winSize];
	self.subLayer1 = [[CCLayerColor alloc] initWithColor:ccc4(255, 0, 0, 100)];
	subLayer1.contentSize = CGSizeMake(winSize.width, winSize.height * 4/5);
	
	//Create the level sprite A
	CCSprite *spriteA = [CCSprite spriteWithFile:@"box_0.png"];	//Create the box sprite
	spriteA.position = ccp(subLayer1.contentSize.width * 1/5, subLayer1.contentSize.height * 4/5);
	spriteA.tag = kTAG_SUB_LAYER_1_A;
	[subLayer1 addChild:spriteA];
	
	//Create the level label A
	CCLabelBMFont *numberLabelA = [CCLabelBMFont labelWithString:@"A" fntFile:@"Dakota_white_16.fnt"];
	numberLabelA.position = CGPointMake(spriteA.contentSize.width/2, spriteA.contentSize.height/2);
	[spriteA addChild:numberLabelA];

	
	
	//Create the level sprite B
	CCSprite *spriteB = [CCSprite spriteWithFile:@"box_0.png"];	//Create the box sprite
	spriteB.position = ccp(subLayer1.contentSize.width * 4/5, subLayer1.contentSize.height * 4/5);
	spriteB.tag = kTAG_SUB_LAYER_1_B;
	[subLayer1 addChild:spriteB];
	
	//Create the level label B
	CCLabelBMFont *numberLabelB = [CCLabelBMFont labelWithString:@"B" fntFile:@"Dakota_white_16.fnt"];
	numberLabelB.position = CGPointMake(spriteB.contentSize.width/2, spriteB.contentSize.height/2);
	[spriteB addChild:numberLabelB];
	
	
	
	//Create the level sprite C
	CCSprite *spriteC = [CCSprite spriteWithFile:@"box_0.png"];	//Create the box sprite
	spriteC.position = ccp(subLayer1.contentSize.width/2, subLayer1.contentSize.height/2);
	spriteC.tag = kTAG_SUB_LAYER_1_C;
	[subLayer1 addChild:spriteC];
	
	//Create the level label C
	CCLabelBMFont *numberLabelC = [CCLabelBMFont labelWithString:@"C" fntFile:@"Dakota_white_16.fnt"];
	numberLabelC.position = CGPointMake(spriteC.contentSize.width/2, spriteC.contentSize.height/2);
	[spriteC addChild:numberLabelC];
	
	
	
	//Create the level sprite D
	CCSprite *spriteD = [CCSprite spriteWithFile:@"box_0.png"];	//Create the box sprite
	spriteD.position = ccp(subLayer1.contentSize.width * 1/5, subLayer1.contentSize.height * 1/5);
	spriteD.tag = kTAG_SUB_LAYER_1_D;
	[subLayer1 addChild:spriteD];
	
	//Create the level label D
	CCLabelBMFont *numberLabelD = [CCLabelBMFont labelWithString:@"D" fntFile:@"Dakota_white_16.fnt"];
	numberLabelD.position = CGPointMake(spriteD.contentSize.width/2, spriteD.contentSize.height/2);
	[spriteD addChild:numberLabelD];	
	
	
	
	//Create the level sprite E
	CCSprite *spriteE = [CCSprite spriteWithFile:@"box_0.png"];	//Create the box sprite
	spriteE.position = ccp(subLayer1.contentSize.width * 4/5, subLayer1.contentSize.height * 1/5);
	spriteE.tag = kTAG_SUB_LAYER_1_E;
	[subLayer1 addChild:spriteE];
	
	//Create the level label E
	CCLabelBMFont *numberLabelE = [CCLabelBMFont labelWithString:@"E" fntFile:@"Dakota_white_16.fnt"];
	numberLabelE.position = CGPointMake(spriteE.contentSize.width/2, spriteE.contentSize.height/2);
	[spriteE addChild:numberLabelE];	

}

-(void) initSubLayer2WithLevel:(NSUInteger)level
{
	CGSize winSize = [[CCDirector sharedDirector] winSize];
	self.subLayer2 = [[CCLayerColor alloc] initWithColor:ccc4(0, 255, 0, 100)];
	subLayer2.contentSize = CGSizeMake(winSize.width, 300);
	subLayer2.position = ccp(0, 100);
	
	for(int row = 0; row < 4; row++)
	{
		for(int col = 0; col < 5; col++)
		{
			NSString* lv = [NSString stringWithFormat:@"%d", 5 * row + col + level];
			
			CCSprite* sprite = [CCSprite spriteWithFile:@"box_0.png"];
			sprite.position = ccp( 50 + col * 50, (4 - row) * 60);
			sprite.tag = [lv intValue];
			[subLayer2 addChild:sprite];
			
			CCLabelTTF* label = [CCLabelTTF labelWithString:lv fontName:@"Marker Felt" fontSize:18];
			[label setPosition:CGPointMake(sprite.contentSize.width/2, sprite.contentSize.height/2)];
			[label setTag:[lv intValue]];
			[sprite addChild:label];
		}
	}
}

-(void) switchToSubLayer2WithLevel:(NSUInteger)level
{
	[self initSubLayer2WithLevel:level];
	
	[subLayer1 removeFromParentAndCleanup:YES];
	[self addChild:subLayer2];
	self.activeSubLayer = subLayer2;
	
}

-(void) backToSubLayer1
{
	[subLayer2 removeFromParentAndCleanup:YES];
	
	[self addChild:subLayer1];
	self.activeSubLayer = subLayer1;
}

-(void) backToMainMenu
{
	[[CCDirector sharedDirector] popScene];
}

#pragma mark - Touch
-(void) registerWithTouchDispatcher
{
	[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
	return YES;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
	CGPoint location = [self convertTouchToNodeSpace:touch];
	
	if(activeSubLayer == subLayer1)
	{
		CCArray *children = [subLayer1 children];
		for(id child in children)
		{
			if( [child isKindOfClass:[CCSprite class]] )
			{
				CCSprite *sprite = (CCSprite *)child;
				
				//Check if the item is selected
				if( CGRectContainsPoint(sprite.boundingBox, location) )
				{
					[self onItemSelected:sprite];
					break;
				}
			}
		}
	}
	else if(activeSubLayer == subLayer2)
	{
		CGPoint newLocation = [subLayer2 convertToNodeSpace:location];
		CCArray *children = [subLayer2 children];
		for(id child in children)
		{
			if( [child isKindOfClass:[CCSprite class]] )
			{
				CCSprite *sprite = (CCSprite *)child;
				
				//Check if the item is selected
				if( CGRectContainsPoint(sprite.boundingBox, newLocation) )
				{
					[self onItemSelected:sprite];
					break;
				}
			}
		}
	}
}

-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
	[self ccTouchEnded:touch withEvent:event];
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
	
}

/*
-(void) initLevelItem:(NSUInteger)totalLevel
{

	
	NSUInteger rowPerPage = 4;
	NSUInteger columnPerPage = 5;
	NSUInteger levelPerPage = rowPerPage * columnPerPage;
	NSUInteger numberOfPage = totalLevel % levelPerPage;
	
	CGPoint subLayerPos = CGPointMake(0, 60);
	CGSize subLayerSize = CGSizeMake( (winSize.width - 40) * numberOfPage , winSize.height * 2/3);
	
	//Create the Layer
	CCLayerColor *subLayer = [[CCLayerColor alloc] initWithColor:ccc4(255, 0, 0, 100)];
	subLayer.position = subLayerPos;
	subLayer.contentSize = subLayerSize;
	
	NSUInteger currentLevel = 1;
	while (currentLevel <= totalLevel)
	{
		NSUInteger currentPage = currentLevel / (levelPerPage + 1);
		NSUInteger currentRow = currentLevel / (rowPerPage + 1);
		NSUInteger currentColumn = (currentLevel - 1) % columnPerPage;
		
		//Create the level sprite
		CCSprite *levelSprite = [CCSprite spriteWithFile:@"box_0.png"];	//Create the box sprite
		CGFloat x = currentPage * subLayer.contentSize.width + currentColumn * 50 + 20;
		CGFloat y = subLayerSize.height - currentRow * 50 - 20;
		levelSprite.position = ccp(x, y);
		[subLayer addChild:levelSprite];
		
		//Create the level label
		CCLabelBMFont *numberLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%d", currentLevel]  fntFile:@"Dakota_white_16.fnt"];
		numberLabel.position = CGPointMake(levelSprite.contentSize.width/2, levelSprite.contentSize.height/2);
		[levelSprite addChild:numberLabel];
		
		//Set Tag
		levelSprite.tag = currentLevel;
		
		currentLevel++;
	}

	[self addChild:subLayer];
}
*/


@end
